package cn.hx.mallorder.service.impl;

import cn.hx.mallorder.dao.OmsOrderDao;
import cn.hx.mallorder.entity.OmsOrder;
import cn.hx.mallorder.service.OmsOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 订单表(OmsOrder)表服务实现类
 *
 * @author makejava
 * @since 2022-11-30 16:05:56
 */
@Service("omsOrderService")
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderDao, OmsOrder> implements OmsOrderService {
    @Resource
    private OmsOrderDao omsOrderDao;

    public List<OmsOrder> findByPay() {
        return omsOrderDao.selectList(new QueryWrapper<OmsOrder>().eq("status",0));
    }

    @Override
    public List<OmsOrder> queryByUserName(String name) {
        return omsOrderDao.queryByUserName(name);
    }

    @Override
    public OmsOrder findAllById(Long id) {
        return omsOrderDao.findAllById(id);
    }

    @Override
    public void deleteById(Long id) {
        omsOrderDao.deleteById(id);
    }

    @Override
    public Long findId(Long i) {
        return omsOrderDao.findId(i);
    }

    @Resource
    RedisTemplate redisTemplate;
    public List<OmsOrder> get(String uid) {
        String keys="order_"+uid+"_*";
        List<OmsOrder> list=new ArrayList<>();
        Set keys1 = redisTemplate.keys(keys);
        for (Object o : keys1) {
            OmsOrder order = (OmsOrder) redisTemplate.opsForValue().get(o);
            Long time = redisTemplate.opsForValue().getOperations().getExpire(o);
            order.setFailTime(time);
            list.add(order);
            System.out.println(time);
        }
        return list;
    }

    public Integer deleteOrder(Long uid,Long oid) {
        String del="order_"+uid+"_"+oid;
        Boolean delete = redisTemplate.opsForValue().getOperations().delete(del);
        if (delete){
            return 1;
        }
        return 0;
    }
}